﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Common;
using MesProjectWinform.Production.Dto.Input.WorkOrder;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using MesProjectWinform.Public.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.WorkOrder
{
    public partial class SplitForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void MyDelegate(); // 定义委托类型
        public MyDelegate MyDelegateCallback; // 定义委托实例
        public SplitForm()
        {
            InitializeComponent();
        }

        public void GetData(string code,int num,object productionlineid)
        {
            
            textEdit1.Text = code;
            textEdit2.Text = num.ToString();
            lookUpEdit1.EditValue = productionlineid;
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            simpleButton2.Enabled= false; // 禁用按钮，防止重复提交
            SplitWorkOrderInput input = new SplitWorkOrderInput()
            {
                EndTime = dateEdit2.DateTime,
                StartTime = dateEdit1.DateTime,
                Productionlineid = lookUpEdit1.EditValue,
                SplitNum = (int)spinEdit1.Value,
                Remark = memoEdit1.Text,
                WorkOrderCode = textEdit1.Text,
            };
            var data = JsonConvert.SerializeObject(input);
            var response = await HttpClientHelper.RequestUrl(RequestType.Post, ProductionGolab.SplitProduct,data);
            var result = JsonConvert.DeserializeObject<ApiResult<int>>(response);
            if(result.Code==ApiEnums.Success)
            {
                this.Close(); // 关闭当前窗体
                MyDelegateCallback?.Invoke(); // 调用委托方法，通知父窗体刷新数据
                MessageBox.Show(result.Message);
            }
            else
            {
                simpleButton2.Enabled = true; // 启用按钮
                MessageBox.Show(result.Message);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SplitForm_Load(object sender, EventArgs e)
        {
            #region 初始化产线下拉框
            lookUpEdit1.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="外壳加工"},
                new TypeModel(){Id=2,Name="骨架加工"},
                new TypeModel(){Id=3,Name="电路板组装"},
                new TypeModel(){Id=4,Name="电池组加工"},
                new TypeModel(){Id=5,Name="机体组装"},
                new TypeModel(){Id=6,Name="动力系统安装"},
                new TypeModel(){Id=7,Name="控制系统安装"},
            };
            lookUpEdit1.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit1.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit1.Properties.PopulateColumns();
            lookUpEdit1.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit1.Properties.NullText = "请选择产线";
            #endregion
        }
    }
}